import { request } from "../../../plugins/api"
import persistance from "../../../store/persistance"
import { aesDecrypt, aesMinEncrypt } from "../../../utils/util"

Page({
	data: {
		//出国留学项目列表
		abroadStudyList: [],
		indexMenuNotShowRedPoint: {}, //控制菜单右上角小红点展示
	},
	onLoad: function (options) {
		this.getAbroadStudyList()
	},
	onShow() {
		const encryptString = aesMinEncrypt("test")
		console.log(encryptString, "encryptString")
		const decryptString = aesDecrypt(encryptString)
		console.log(decryptString, "decryptString")

		const indexMenuNotShowRedPoint = persistance.indexMenuNotShowRedPoint.get()
		this.setData({
			indexMenuNotShowRedPoint: indexMenuNotShowRedPoint,
		})
	},
	//首页点击率统计
	homePageClickStatistics(event) {
		const channel = event.currentTarget.dataset.channel
	},
	//跳转到h5页面
	jumpH5Page(event) {
		this.homePageClickStatistics(event)
		wx.navigateTo({
			url:
				"/pages/web/web?path=" +
				encodeURIComponent(event.currentTarget.dataset.path),
		})
	},
	//跳转到别的小程序页面
	jumpMiniProgramPage(event) {
		this.homePageClickStatistics(event)
		//当前的小程序的版本
		const env = wx.getAccountInfoSync().miniProgram.envVersion
		//要跳转的小程序的版本
		let envVersion
		//开发环境跳到体验版小程序
		if (["develop", "trial"].includes(env)) {
			envVersion = "trial"
			//生产环境跳到正式版小程序
		} else {
			envVersion = "release"
		}
		wx.navigateToMiniProgram({
			appId: event.currentTarget.dataset.appid,
			path: event.currentTarget.dataset.path,
			extraData: {
				from: "xwx",
			},
			envVersion,
		})
	},
	//跳转到自身的小程序的页面
	jumpMiniProgramSelfPage(event) {
		this.homePageClickStatistics(event)
		wx.navigateTo({
			url: event.currentTarget.dataset.path,
		})
	},
	//文件代办、文件翻译、粤东公证、刷题鱼菜单点击
	menuItemClick(event) {
		//控制菜单右上角小红点展示
		persistance.indexMenuNotShowRedPoint.add(event.currentTarget.dataset.name)
		//刷新数据
		this.setData({
			indexMenuNotShowRedPoint: persistance.indexMenuNotShowRedPoint.get(),
		})
		//执行后置事件
		this[event.currentTarget.dataset.event](event)
	},
	//获取出国留学项目列表
	getAbroadStudyList() {
		wx.showLoading({
			title: "加载中",
		})
		function errorHandle(err) {
			console.log(err, "获取出国留学项目列表失败")
		}
		wx.request({
			url: "https://xwx.gzzmedu.com:10086/enrollmentClient/getProjectPage",
			method: "POST",
			data: {},
			dataType: "json",
			success: (res) => {
				//请求处理成功
				if (res.data.errcode === "0") {
					let list = res.data.result.data
					//截取前三个元素
					if (list.length > 3) {
						list.splice(3)
					}
					//将一维数组按照每2个元素转为二维数组用于页面渲染
					let newList = []
					for (let i = 0; i < list.length; i += 2) {
						newList.push(list.slice(i, i + 2))
					}
					this.setData({
						abroadStudyList: newList,
					})
					//请求异常
				} else {
					errorHandle(res)
				}
			},
			fail: (err) => {
				errorHandle(err)
			},
			complete: () => {
				wx.hideLoading()
			},
		})
	},
})
